﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zk312.MODEL;

namespace zk312.DAL
{
    public class Roomdal
    {
        public Roomdal(MyContext context)
        {
            Context = context;
        }

        public MyContext Context { get; }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="room"></param>
        /// <returns></returns>
        public int AddRoom(Room room)
        {
            try
            {
                Context.Rooms.Add(room);
                return Context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public dynamic GetRoom(int index,int size,string? name,string? type)
        {
            try
            {
                var m = Context.Rooms.AsQueryable();
                if (name != null)
                {
                    m = m.Where(x => x.name == name);
                }
                if (type != null)
                {
                    m = m.Where(x => x.type == type);
                }
                m=m.Where(x => x.lj==0);
                var c = m.Count();
                var d = m.OrderBy(x => x.num).Skip((index - 1) * size).Take(size).ToList();

                var res = new { count = c, data = d };
                return res;
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 逻辑删
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public dynamic DelRoom(int id)
        {
            try
            {
                var m = Context.Rooms.FirstOrDefault(x => x.id == id);
                Context.Rooms.Update(m);
                m.lj = 1;
                return Context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 修改第一步 先查
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public dynamic Getid(int id)
        {
            try
            {
                var m = Context.Rooms.FirstOrDefault(x => x.id == id);
                return m;
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 修改第二部 修改
        /// </summary>
        /// <param name="room"></param>
        /// <returns></returns>
        public dynamic UpdRoom(Room room)
        {
            try
            {
                Context.Rooms.Update(room);
                return Context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }

        }







    }

}
